package com.tm.requestexample.controller;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@RestController
@RequestMapping("student")
public class StuController {


    @RequestMapping("getById")
    public Student getById(Integer id){

        return new Student(1,"张三");
    }

    /**
     * @RequestBody   接受body的参数
     * @RequestParam  接收请求路径后的问号后面的参数
     * @PathVariable  可以用来映射URL中的占位符到目标方法的参数中
     *
     */
    @RequestMapping(path = "save",method = RequestMethod.POST)
    public Student save(@RequestBody  Student student){

        return student;
    }

    @GetMapping("getById2")
    public Student getById2(@RequestParam Integer id,
                            @RequestParam String name){

        return new Student(id,name);
    }

    @PostMapping("getById3")
    public Student getById3(@RequestParam Integer id,@RequestParam String name){

        return new Student(id,name);
    }

    @GetMapping("getById4/{id}/{name}")
    public Student getById4(@PathVariable Integer id,@PathVariable String name){

        return new Student(id,name);
    }

    @PostMapping("getById5/{id}/{name}")
    public Student getById5(@PathVariable Integer id,@PathVariable String name){

        return new Student(id,name);
    }

    /**
     * TODO 错误示范

    @GetMapping("getById6")
    public List<Integer> getById6(List<Integer> ids){

        return ids;
    }  */

    @GetMapping("getById7")
    public List<Integer> getById(@RequestParam List<Integer> ids){

        return ids;
    }

    /**
     * 容器
     */
    static  Map<String,Object> map = new ConcurrentHashMap<>();

    /**
     * 每次注入bean的时候 ，将我们注入的对象放入到map集合中
     *
     *
     */
    @GetMapping("getById8/{ids}")
    public List<Integer> getById8(@PathVariable List<Integer> ids){
        Student student = (Student) map.get("student");
        return ids;
    }


    @PostMapping("getById9")
    public List<Integer> getById9(@RequestBody List<Integer> ids){
        Student student = new Student(2,"王五");
        map.put("student",student);
        return ids;
    }

}

@Data
@AllArgsConstructor
@NoArgsConstructor
class Student{
    private Integer id;
    private String name;
}